<template>
	<view>
		<select-model ref="model" size="mini">
			<!-- <template v-slot:default>
				<view class="button_view" v-if="uni.vk.getVuex("$user.userInfo.avatar")==''"></view>
			</template> -->
			<template v-slot:dataview>
				<view style=" display: flex;justify-content: center;align-items: center;">
					<set-user-info @updateUserInfo="action" :submitText="typeCN"></set-user-info>
				</view>
			</template>
		</select-model>
	</view>
</template>

<script>
import { getUserInfo } from '../../API/getUserInfo.js';import {login} from '../../API/login.js'
	import userStore from '../../utils/userStore';
	import selectModel from './select-model.vue'
	import setUserInfo from './set-user-info.vue'
	export default {
		name: "by-nike-avatar",
		components: {
			selectModel,
			setUserInfo
		},
		// 监听 - 页面每次【加载时】执行(如：前进)
		onLoad(options = {}) {
			// vk = uni.vk;
			this.options = options;

		},
		mounted() {
			this.init();
		},
		onShow() {
			this.init();
		},
		data() {
			return {
				typeCN: '登陆',
			};
		},
		methods: {
			init() {
				if (userStore.get()) this.typeCN = '修改';
			},
			async loginAndUpdate(userInfo) {
				var _this = this;
				
				await login(userInfo);
				await getUserInfo();
				_this.$refs.model.changeShow();
				_this.typeCN = '修改';
				_this.$emit('update:userInfo',userStore.get())
				

			},
			async update(userInfo) {
				let _this = this;
				
				await login(userInfo);
				await getUserInfo();
				_this.$refs.model.changeShow();
				_this.typeCN = '修改';
				_this.$emit('update:userInfo',userStore.get())
				
			},
			action(userInfo) {
				switch (this.typeCN) {
					case '登陆':
						this.loginAndUpdate(userInfo);
						break;
					case '修改':
						this.update(userInfo);
						break;
				}
			}
		}
	}
</script>

<style>
	.button_view {
		position: absolute;
		width: 100%;
		height: 100%;
		left: 0;
		top: 0;
	}
</style>